#include "lib\\SDL2\\include\\SDL.h"
#include "lib\\SDL2\\include\\SDL_image.h"

#pragma comment(lib, "lib\\SDL2\\lib\\x86\\SDL2main.lib")
#pragma comment(lib, "lib\\SDL2\\lib\\x86\\SDL2.lib")
#pragma comment(lib, "lib\\SDL2\\lib\\x86\\SDL2test.lib")
#pragma comment(lib, "lib\\SDL2\\lib\\x86\\SDL2_image.lib")

#include <stdio.h>
#include <math.h>

#define WINDOW_W 270
#define WINDOW_H 480
#define PIC_ZOOM 4

bool isRunning = true;

SDL_Window* pWindow = nullptr;

SDL_Renderer* pRender = nullptr;

SDL_Texture* shot = nullptr;

int pos_x, pos_y;


void jumpto(int x, int y);

void fleshscreenshot();


#undef main  //解决链接导致编译错误问题
int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
    IMG_Init(IMG_INIT_PNG);

    pWindow = SDL_CreateWindow("jump", 50,50,
                                WINDOW_W, WINDOW_H,
                                0);
    pRender = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(pRender, 0, 0, 0, 0xFF);
    SDL_RenderClear(pRender);
    SDL_RenderPresent(pRender);

    shot = IMG_LoadTexture(pRender, "jump.png");
    SDL_RenderCopy(pRender, shot, nullptr, nullptr);
    SDL_RenderPresent(pRender);

    SDL_Event e;

    int m_x, m_y;

    //Uint8* state;
    Uint32 mstate = 0;

    while(SDL_WaitEvent(&e) && isRunning) {
        switch (e.type) {
            case SDL_QUIT:
                isRunning = false;
            break;
            case SDL_MOUSEBUTTONDOWN:
                mstate = SDL_GetMouseState(&m_x, &m_y);
                //printf("x y state %d %d %d\n", m_x, m_y, mstate);
                if(mstate == 1) {
                    pos_x = m_x;
                    pos_y = m_y;
                    printf("set myself pos %d %d\n", m_x, m_y);
                } else if (mstate == 4) {
                    jumpto(m_x, m_y);
                    printf("target pos %d %d\n", m_x, m_y);
                    SDL_Delay(1000);
                    fleshscreenshot();
                }
                
            break;
            case SDL_KEYDOWN:
                const Uint8* state = SDL_GetKeyboardState(NULL);
                //printf("keyboard %d %d\n", state[SDL_SCANCODE_RETURN] , state[SDL_SCANCODE_BACKSPACE]);
                if(state[SDL_SCANCODE_RETURN]) {
                    //printf("get here\n");
                    fleshscreenshot();
                    
                }
            break;
            
        }
    }

    SDL_DestroyTexture(shot);
    SDL_DestroyRenderer(pRender);
    SDL_DestroyWindow(pWindow);
    IMG_Quit();
    SDL_Quit();
    return 0;
}

int calcdistance(int sx, int sy, int dx, int dy) {
    int lx = abs(dx - sx) * PIC_ZOOM;
    int ly = abs(dy - sy) * PIC_ZOOM;
    return sqrt(lx*lx + ly*ly);
}

void jumpto(int x, int y) {
    char cmd[100];
    //1.392 magic number
    int presstime = calcdistance(pos_x, pos_y, x, y) * 1.392;

    sprintf(cmd, "adb shell input swipe 500 1600 500 1602 %d", presstime);
    printf("%s", cmd);
    system(cmd);
}

void fleshscreenshot() {
    printf("adb shell screencap -p /sdcard/jump.png");
    //system("del jump.png");
    //SDL_Delay(1000);
    system("adb shell screencap -p /sdcard/jump.png");
    //SDL_Delay(1000);
    system("adb pull /sdcard/jump.png");
    //SDL_Delay(1000);
    system("adb shell rm /sdcard/jump.png");

    SDL_DestroyTexture(shot);
    shot = IMG_LoadTexture(pRender, "jump.png");
    SDL_RenderCopy(pRender, shot, nullptr, nullptr);
    SDL_RenderPresent(pRender);
}
